
/**
 * MyGridViewAdapter.java
 * @author Jason Lu
 * @date 2013-10-17
 * @version 1.0
 */
package com.x52im.mall.logic.shop;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.x52im.mall.shop.dto.Color;
import com.x52im.rbchat.R;

/**
 * @author Jason
 * 
 */
public class MyGridViewAdapter extends BaseAdapter
{

	private Context context;// 运行上下文
	private List<Color> listItems;// 数据集合
	private LayoutInflater listContainer;// 视图容器
	private int itemViewResource;// 自定义项视图源

	static class GridItemView
	{
		// 自定义控件集合
		public TextView colorBtn;
	}

	public MyGridViewAdapter(Context context, List<Color> data, int resource)
	{
		this.context = context;
		this.listContainer = LayoutInflater.from(this.context); // 创建视图容器并设置上下文
		this.itemViewResource = resource;
		this.listItems = data;
	}

	public int getCount()
	{
		return listItems.size();
	}

	public Object getItem(int arg0)
	{
		return null;
	}

	public long getItemId(int arg0)
	{
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		// 自定义视图
		GridItemView gridItemView = null;

		if (convertView == null)
		{
			// 获取list_item布局文件的视图
			convertView = listContainer.inflate(this.itemViewResource, null);
			gridItemView = new GridItemView();
			// 获取控件对象
			gridItemView.colorBtn = (TextView) convertView.findViewById(R.id.common_mall_shop_layout_good_detail_girdview_item_radio);

			// // 设置控件集到convertView
			convertView.setTag(gridItemView);
			if (listItems != null)
			{
				Color c = listItems.get(position);
				gridItemView.colorBtn.setText(c.getColor_desc());
				gridItemView.colorBtn.setTag(c);
			}
			if(position == 0)
				convertView.setBackgroundDrawable(convertView.getResources().getDrawable(R.drawable.common_good_detail_color_selected));

		}
		else
		{
			gridItemView = (GridItemView) convertView.getTag();
		}

		return convertView;
	}

}
